package com.syqc.login

import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.*

/**
 *  MD5加密工具类
 */
class MD5Utils {
    ////生成随机数字和字母, length为位数
    fun getRandomMD5(length: Int): String {
        var chart = ""
        val random = Random()
        // 参数length，表示生成几位随机数
        for (i in 0 until length) {
            val charOrNum = if (random.nextInt(2) % 2 == 0) "char" else "num"
            // 输出字母还是数字
            if ("char".equals(charOrNum, ignoreCase = true)) { // 输出是大写字母还是小写字母
                val temp = if (random.nextInt(2) % 2 == 0) 65 else 97
                chart += (random.nextInt(26) + temp).toChar()
            } else if ("num".equals(charOrNum, ignoreCase = true)) {
                chart += random.nextInt(10).toString()
            }
        }
        return chart
    }

    companion object {
        private val hexDigits = charArrayOf(
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F'
        )

        /**
         * 字符串 MD5 加密
         *
         * @param text ：文本内容
         * @return　加密后的内容
         */
        fun encrypt(text: String): String? {
            var result: String? = null
            try {
                val md = MessageDigest.getInstance("MD5")
                result = byte2hex(md.digest(text.toByteArray()))
            } catch (e: NoSuchAlgorithmException) {
                e.printStackTrace()
            }
            return result
        }

        /**
         * 加密辅助
         *
         * @param b : 内容二进制数组
         * @return 加密大写十六进制
         */
        private fun byte2hex(b: ByteArray): String {
            var hs = ""
            var stmp: String
            for (n in b.indices) {
                stmp = Integer.toHexString(b[n].toInt() and 0XFF)
                hs = if (stmp.length == 1) hs + "0" + stmp else hs + stmp
            }
            return hs.toUpperCase()
        }

        /**
         * 生成全以大写字母
         *
         * @param inStr
         * @return 32byte MD5 Value
         */
        fun getMD5(inStr: String): String? {
            val inStrBytes = inStr.toByteArray()
            try {
                val md = MessageDigest.getInstance("MD5")
                md.update(inStrBytes)
                val digest = md.digest()
                val str = CharArray(digest.size * 2)
                var k = 0
                for (i in digest.indices) {
                    val temp = digest[i].toInt()
                    str[k++] = hexDigits[temp ushr 4 and 0xf]
                    str[k++] = hexDigits[temp and 0xf]
                }
                return String(str)
            } catch (e: NoSuchAlgorithmException) {
                e.printStackTrace()
            }
            return null
        }

        //生成全以小写字母
        fun getmd5(str: String): String {
            try {
                val md = MessageDigest.getInstance("MD5")
                md.update(str.toByteArray())
                val digest = md.digest()
                val sb = StringBuffer()
                for (i in digest.indices) {
                    var v = digest[i].toInt()
                    v = if (v < 0) 0x100 + v else v
                    val cc = Integer.toHexString(v)
                    if (cc.length == 1) sb.append('0')
                    sb.append(cc)
                }
                return sb.toString()
            } catch (e: Exception) {
            }
            return ""
        }
    }
}